<!DOCTYPE html>
<html>
	<head>
	    <meta charset="UTF-8">
	    <meta name="viewport" content="width=device-width,
	    user-scalable=no,initial-scale=1.0,maximum=1.0,minimum=1.0">
	    <title>授权</title>
	    <!-- 引入jquery-->
	    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
	    <!-- 引入 1.3.2的js-sdk文件 -->
	    <script src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
	</head>
	 
	<body>
	 <div>1212</div>
	</body>
	<script>
	    function getCode() {
			debugger
	        var code = "";
	        var local = window.location.href; // 获取页面url
	        var appid = "wxcd61c92313ae44af";
	        code = getUrlCode().code; // 截取code
	        if (code == null || code === "") {
	            // 如果没有code，则去请求
	            window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(
	                local
	            )}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`;
				console.log(code)
	            // scope=snsapi_base      静默授权,自动跳转到回调页的  特点：用户无感知；
	            // scope=snsapi_userinfo  非静默授权，第一次有弹框
	        } else {
				console.log(code)
	            // 你自己的业务逻辑
	            // 在这里跳回自己的小程序，并且把获取到的code传递给小程序
	            //参考文档：https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html
	            wx.miniProgram.redirectTo({ 
	                url: '/my_pages/resCode/resCode?code=' + code
	            })
	        }
	    }
	    getCode()
	    // 截取url中的code方法
	    function getUrlCode() {
	        var url = location.search;
	        var theRequest = new Object();
	        if (url.indexOf("?") != -1) {
	            var str = url.substr(1);
	            var strs = str.split("&");
	            for (var i = 0; i < strs.length; i++) {
	                theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
	            }
	        }
	        return theRequest;
	    }
	</script>
</html>
